<?php


namespace App\Services;


class WechatService
{
    public $app;

    public function __construct()
    {
        $this->app = app('wechat.mini_program');
    }

    public function get($path, $options)
    {
        if (!empty($options)) {
            $response = $this->app->app_code->get($path, $options);
        } else {
            $response = $this->app->app_code->get($path);
        }
        $unique_file = uniqid();
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs(public_path("images/qrcode"), $unique_file . '.png');
        }
        return config('app.url').public_path("images/qrcode/".$filename);

    }

    public function getUnlimit(string $scene, $path, array $optional = [])
    {

        $response = $this->app->app_code->getUnlimit($scene, [
            'page' => $path,
            'width' => $optional['width'],
            "is_hyaline"=>true
        ]);
        $unique_file = uniqid();
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs(public_path("images/qrcode"), $unique_file . '.png');
        }
        return config('app.url')."/images/qrcode/".$filename;
    }
}
